﻿using System;
using System.Data;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.QueryManager.Core;
using br.com.SistemaChamados.QueryManager.Core.Behavior;

namespace br.com.SistemaChamados.DAO
{
    public class AnnexDAO
    {
        private static string SQL_INSERT_ANNEX = " INSERT INTO TBL_ANNEX (TBL_ANNEXDESCRIPTION, TBL_CALLID) VALUES (@annexdescription, @callid) ";
        private static string SQL_GET_ANNEX_CALL = " SELECT * FROM TBL_ANNEX WHERE TBL_CALLID = @callid";
        private static string SQL_DELETE_ANNEX = " DELETE FROM TBL_ANNEX WHERE TBL_ANNEXID = @annexid";

        public static EnumCRUDState DeleteAnnex(Annex annex)
        {
            return (EnumCRUDState)QueryManagerPerformer.Delete(annex, ConnectionBehavior.CONNECTION_BEHAVIOR_CLOSE_AFTER_OPERATION);
        }

        public static Annex GetAnnexByCall(Call call)
        {
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            Annex annexReturn = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_ANNEX_CALL, conn);
                command.Parameters.Add(Mapped.Parameter("@callid", call.Id));
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    annexReturn = new Annex();
                    annexReturn.Id = Convert.ToInt32(reader["TBL_ANNEXID"].ToString());
                    string annexDescription = Convert.ToString(reader["TBL_ANNEXDESCRIPTION"].ToString());
                    int lastIndexOfBar = annexDescription.LastIndexOf(@"\") + 1;
                    int annexDescriptionLenght = annexDescription.Length;
                    annexDescription = annexDescription.Substring(lastIndexOfBar, (annexDescriptionLenght - lastIndexOfBar));
                    annexReturn.Description = annexDescription;
                    annexReturn.Call = call;
                }

            }
            catch (Exception ex)
            {
                annexReturn = null;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return annexReturn;
        }

        public EnumCRUDState InsertAnnex(Annex annex)
        {
            EnumCRUDState resultAnnex = EnumCRUDState.SQL_NOTHING;
            IDbConnection conn = null;
            IDbCommand command = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_INSERT_ANNEX, conn);
                command.Parameters.Add(Mapped.Parameter("@annexdescription", annex.Description));
                command.Parameters.Add(Mapped.Parameter("@callid", annex.Call.Id));
                command.ExecuteNonQuery();
                resultAnnex = EnumCRUDState.SQL_SUCCESS;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return resultAnnex;
        }
    }
}